﻿@{
    ViewData["Title"] = "节假日设置";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title">节假日设置</h3>
                </div>
                <div class="card-body">
                    <form id="holidayForm">
                        <input type="hidden" id="holidayId" name="Id" />

                        <div class="form-group">
                            <label for="holidayDate">日期</label>
                            <input type="date" class="form-control" id="holidayDate" name="HolidayDate" required>
                        </div>

                        <div class="form-group">
                            <label>类型</label>
                            <div class="form-check">
                                <input class="form-check-input" type="radio" name="HolidayType" id="holidayType0" value="0" checked>
                                <label class="form-check-label text-danger" for="holidayType0">
                                    假日
                                </label>
                            </div>
                            <div class="form-check">
                                <input class="form-check-input" type="radio" name="HolidayType" id="holidayType1" value="1">
                                <label class="form-check-label text-primary" for="holidayType1">
                                    班（工作日）
                                </label>
                            </div>
                        </div>

                        <div class="form-group">
                            <button type="button" class="btn btn-primary" onclick="saveHoliday()">保存</button>
                            <button type="button" class="btn btn-secondary" onclick="closeModal()">取消</button>
                            <button type="button" class="btn btn-danger" id="deleteBtn" onclick="deleteHoliday()" style="display:none;">删除</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(document).ready(function() {
        var urlParams = new URLSearchParams(window.location.search);
        var date = urlParams.get('date');

        if (date) {
            $('#holidayDate').val(date);
            // 查询是否已有该日期的设置
            loadHolidayData(date);
        }
    });

    // 加载已有的节假日数据
    function loadHolidayData(date) {
        $.get('/HolidaySetting/GetHolidayByDate', { date: date })
            .done(function(response) {
                if (response.success && response.data) {
                    // 有数据，显示为编辑模式
                    $('#holidayId').val(response.data.id);
                    $('#holidayDate').val(response.data.holidayDate.split('T')[0]);
                    $('input[name="HolidayType"][value="' + response.data.holidayType + '"]').prop('checked', true);
                    $('#deleteBtn').show();
                    $('.card-title').text('修改节假日设置');
                } else {
                    // 无数据，显示为添加模式
                    $('.card-title').text('添加节假日设置');
                }
            })
            .fail(function() {
                alert('加载数据失败');
            });
    }

    // 保存节假日设置
    function saveHoliday() {
        var formData = {
            Id: $('#holidayId').val() || null,
            HolidayDate: $('#holidayDate').val(),
            HolidayType: parseInt($('input[name="HolidayType"]:checked').val())
        };

        if (!formData.HolidayDate) {
            alert('请选择日期');
            return;
        }

        $.ajax({
            url: '/HolidaySetting/SaveHolidaySetting',
            type: 'POST',
            contentType: 'application/json',
            data: JSON.stringify(formData),
            success: function(response) {
                if (response.success) {
                    alert('保存成功');
                    if (window.opener) {
                        window.opener.location.reload(); // 刷新父页面
                        window.close();
                    } else {
                        window.location.href = '/HolidaySetting';
                    }
                } else {
                    alert('保存失败：' + response.message);
                }
            },
            error: function() {
                alert('保存失败，请重试');
            }
        });
    }

    // 删除节假日设置
    function deleteHoliday() {
        if (confirm('确定要删除这个节假日设置吗？')) {
            var date = $('#holidayDate').val();

            $.post('/HolidaySetting/DeleteHolidaySetting', { date: date })
                .done(function(response) {
                    if (response.success) {
                        alert('删除成功');
                        if (window.opener) {
                            window.opener.location.reload(); // 刷新父页面
                            window.close();
                        } else {
                            window.location.href = '/HolidaySetting';
                        }
                    } else {
                        alert('删除失败：' + response.message);
                    }
                })
                .fail(function() {
                    alert('删除失败，请重试');
                });
        }
    }

    // 关闭模态框或页面
    function closeModal() {
        if (window.opener) {
            window.close();
        } else {
            window.location.href = '/HolidaySetting';
        }
    }
</script> 